----------------------------------Spell Variables--------------------------------------
SPELL.m_iSpellID = 48311 -- Whatever value you want as long as it doesn't override any other spells' IDs.
SPELL.m_iRange = 240
SPELL.m_sName = "Machine Gun Punch"
SPELL.m_sToolTip = [[Punches rapidly in front of
					you for 1.5 seconds. Activation
					mid-channel causes an uppercut.]]
SPELL.m_sIcon = "devin/blackbetty/spells/brawler/multipunch.png"
SPELL.m_sReplaceIcon = "devin/blackbetty/spells/brawler/uppercut.png"
SPELL.m_bRequiresTarget = true
SPELL.m_iCoolDown = 12
SPELL.m_iSpellType = SPELLTYPE_MELEEDAMAGE
SPELL.m_bRecastWhileCasting = true

---------------------------------------------------------------------------------------

local range = 100
local weapondamage = 10
local uppercut_time = false

function SPELL:Cast( pl, target )
	pl:SetNWString("HoldType", "melee2")
	
	if(target && target:IsValid() and target != pl and pl:GetPos():Distance(target:GetPos()) < self.m_iRange) then
		pl.InSpellAnimation = true
		net.Start( "SendPlayerCast" )
		local SendTable = {3.4, self.m_sName, Color(15, 15, 155, 155)}
		net.WriteTable(SendTable)
		net.Send(pl)
		pl:SetVelocity(pl:GetVelocity()*-1) 
		ParticleEffectAttach( "whirlwind", PATTACH_POINT_FOLLOW, pl, pl:LookupAttachment("anim_attachment_RH"))
		pl.Speed = true	
			pl.SpeedMod = -1
			pl.SpeedModDuration = 3
			pl.SpeedType = "Charge"
			pl:EmitSound("devin/blackbetty/weapons/atata.wav", 160, 100)
			timer.Create(""..pl:EntIndex().."multipunch", 0.1, 34, function()
				timer.Simple(0.01, function() pl:DoAnimationEvent(ACT_HL2MP_GESTURE_RANGE_ATTACK_FIST) end)
				--target:EmitSound("devin/blackbetty/weapons/swing/swing_hit.wav", 160, 100+math.random(10,60))
				if(SERVER)then
					timer.Simple(0.1, function() 
						if(target and target:IsValid() and pl:GetPos():Distance(target:GetPos()) < self.m_iRange)then
							ParticleEffect( "pummel_hit", target:GetPos() + Vector(0, 0, 48), Angle(0,0,0), target )
							--target:EmitSound("devin/blackbetty/weapons/pummel/pummel.wav", 160, 100+math.random(30,60))
							local dmginfo = DamageInfo()
								dmginfo:SetDamage( 2 ) --50 damage
								dmginfo:SetDamageType( DMG_GENERIC ) --Bullet damage
								dmginfo:SetAttacker( pl ) --First player found gets credit
								dmginfo:SetDamageForce( Vector( 0, 0, 1000 ) ) --Launch upwards
							target:TakeDamageInfo( dmginfo ) 							
						end
					end)
				end
			end)
			timer.Create(""..pl:EntIndex().."multipunchuppercut", 3.4, 1, function()
			pl:SetNWString("HoldType", "fist")
			pl:SetNWBool("Casting", false)
			pl:SetNWBool("Casted", false)
			pl.InSpellAnimation = false
			net.Start("UseIconFromSpell")
			net.WriteFloat(self.m_iSpellID)
			net.Send(pl)
			end)
		end
	end
	


function SPELL:Recast( pl, target )
	if(timer.Exists(""..pl:EntIndex().."multipunch"))then
		timer.Destroy(""..pl:EntIndex().."multipunch")
		if(timer.Exists(""..pl:EntIndex().."multipunchuppercut"))then
			timer.Destroy(""..pl:EntIndex().."multipunchuppercut")
		end
		net.Start( "CancelPlayerCast" )
		net.Send(pl)
	end
		pl.InSpellAnimation = true
		pl:DoAnimationEvent(ACT_MP_WALK_PDA)
		pl:EmitSound("devin/blackbetty/weapons/watah.wav", 160, 100)
		pl:EmitSound("devin/blackbetty/player/male/swing_common"..math.random(1,3)..".wav", 100, 100+math.random(-1, -5))
		pl:EmitSound("devin/blackbetty/weapons/slam/swing_"..math.random(1,3)..".wav", 160, 100+math.random(20,30))
			timer.Simple(0.1, function()
				if(target and target:IsValid() and pl:GetPos():Distance(target:GetPos()) < self.m_iRange)then
					if(SERVER)then
						timer.Simple(0.1, function() 
							ParticleEffect( "pummel_hit", target:GetPos() + Vector(0, 0, 48), Angle(0,0,0), target )
							target:EmitSound("devin/blackbetty/weapons/pummel/pummel.wav", 160, 100+math.random(30,60))
							local dmginfo = DamageInfo()
								dmginfo:SetDamage( 15 ) --50 damage
								dmginfo:SetDamageType( DMG_SLASH ) --Bullet damage
								dmginfo:SetAttacker( pl ) --First player found gets credit
								dmginfo:SetDamageForce( Vector( 0, 0, 1000 ) ) --Launch upwards
							target:TakeDamageInfo( dmginfo ) 
							target:SetVelocity(((pl:GetPos()- Vector(0, 0, 80)) - target:GetPos())*-4)
						end)
					end
				end
			end)
		timer.Simple(0.55, function()
			pl.InSpellAnimation = false
			pl:SetNWBool("Casting", false)
			pl:SetNWBool("Casted", false)
		end)
end

